package com.itheima.controller;

import com.itheima.domain.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itheima.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class UserController {


    @Autowired
    private UserService userService;

    /**
     * 测试方法
     */
    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        System.out.println("UserController.hello()");
        return "ok";
    }

    @RequestMapping("/add")
    public String add() {
        return "/user/add";
    }

    @RequestMapping("/update")
    public String update() {
        return "/user/update";
    }

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "/login";
    }

    @RequestMapping("/noAuth")
    public String noAuth() {
        return "/noAuth";
    }

    /**
     * 测试thymeleaf
     */
    @RequestMapping("/testThymeleaf")
    public String testThymeleaf(String name, Model model) {
        //把数据存入model
        model.addAttribute("name", name);
        //返回test.html
        return "test";
    }


    @RequestMapping("/index")
    @ResponseBody
    public String index() {
        return "hello";
    }

    /**
     * 登录逻辑处理
     */
    @RequestMapping("/login")
    public String login(String name, String password, Model model, HttpServletRequest request) {
        System.out.println("name=" + name);
        /**
         * 使用Shiro编写认证操作
         */
        //1.获取Subject
        Subject subject = SecurityUtils.getSubject();

        //2.封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(name, password);

        //3.执行登录方法
        try {
            subject.login(token);

            //登录成功
            //跳转到test.html
            return "redirect:/testThymeleaf?name=" + name;
        } catch (UnknownAccountException e) {
            //e.printStackTrace();
            //登录失败:用户名不存在
            model.addAttribute("msg", "用户名不存在");
            return "login";
        } catch (IncorrectCredentialsException e) {
            //e.printStackTrace();
            //登录失败:密码错误
            model.addAttribute("msg", "密码错误");
            return "login";
        }
    }

    @RequestMapping("/toRegister")
    public String toRegister() {
        return "register";
    }

    /**
     * 注册逻辑处理
     */
    @RequestMapping("/register")
    public String register(User user, Model model, HttpServletRequest request, HttpServletResponse response) {
        if(user.getName()==null || user.getName()==""){
            model.addAttribute("res","注册失败,用户名不可为空");
            return "register";
        }
        if(user.getPassword()==null || user.getPassword()==""){
            model.addAttribute("res","注册失败,密码不可为空");
            return "register";
        }


        try {
            userService.addUser(user);

        } catch (org.springframework.dao.DuplicateKeyException e) {
            e.printStackTrace();
            model.addAttribute("res","注册失败,用户名已存在！");
            return "register";
        }
        return "redirect:/toLogin";
    }
}
